<?php

// @author 3ogx <3ogx.com@gmail.com>

namespace Jushuitan;

class ErpBase
{
    /**
     * @var array
     */
    protected $config = [];

    /**
     * @var array
     */
    protected $publicRequestParams = [
    ];

    protected $client;

    /**
     * @var string
     */
    protected $getAccessTokenUrl = 'https://openapi.jushuitan.com/openWeb/auth/accessToken';

    /**
     * @var string
     */
    protected $refreshTokenUrl = 'https://openapi.jushuitan.com/openWeb/auth/refreshToken';

    /**
     * @return array
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * @return $this
     */
    public function setConfig(array $config)
    {
        $this->config['app_key']      = $config['app_key'];
        $this->config['app_secret']   = $config['app_secret'];
        $this->config['access_token'] = $config['access_token'];
        // $this->config['authUrl']      = 'https://openweb.jushuitan.com/auth';
        // $this->config['baseUrl']      = 'https://openweb.jushuitan.com';
        $this->config['authUrl']      = 'https://dev-api.jushuitan.com/auth';
        $this->config['baseUrl']      = 'https://dev-api.jushuitan.com';
        $this->config['version']      = '2';

        return $this;
    }

    /**
     * @return array
     */
    public function getPublicRequestParams()
    {
        return $this->publicRequestParams;
    }

    public function setPublicRequestParams(array $params): void
    {
        if (! isset($params['access_token']) || empty($params['access_token'])) {
            throw new ErpException('缺少 access_token 必要参数!');
        }
        $this->publicRequestParams = [
            'app_key'      => $params['app_key'] ?? $this->getConfig()['app_key'],
            'access_token' => $params['access_token'],
            'charset'      => 'utf-8',
            'version'      => $params['version'] ?? $this->getConfig()['version'],
            'timestamp'    => now()->timestamp,
        ];
    }
}
